﻿using System;

namespace dPet.Domain.Models
{
	using Hangerd;
	using Hangerd.Entity;

	public class ProductInventory : EntityBase
	{
		#region Public Properties

		/// <summary>
		/// 商品
		/// </summary>
		public virtual Product Product { get; private set; }

		/// <summary>
		/// 库存编号
		/// </summary>
		public string InventoryNumber { get; private set; }

		/// <summary>
		/// 库存量
		/// </summary>
		public int Quantity { get; private set; }

		/// <summary>
		/// 过期时间
		/// </summary>
		public DateTime ExpiryTime { get; private set; }

		#endregion

		#region Constructors

		private ProductInventory() { }

		public ProductInventory(Product product, DateTime expiryTime)
		{
			if (expiryTime <= DateTime.Now)
			{
				throw new HangerdException("过期时间不可早于当前时间");
			}

			this.Product = product;
			this.InventoryNumber = string.Format("PI-{0}-{1}", product.Id.Substring(0, 6).ToUpper(), expiryTime.ToString("yyyyMMdd"));
			this.ExpiryTime = expiryTime;
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// 库存记录增加相应数量
		/// </summary>
		public void Add(int quantity)
		{
			if (quantity <= 0)
			{
				throw new HangerdException("商品:{0} 入库量须大于0", this.Product.Name);
			}

			this.Quantity += quantity;
		}

		/// <summary>
		/// 库存记录减少相应数量
		/// </summary>
		public void Reduce(int quantity)
		{
			if (this.Quantity < quantity)
			{
				throw new HangerdException("商品:{0} 库存不足，库存编号:{1}", this.Product.Name, this.InventoryNumber);
			}

			this.Quantity -= quantity;
		}

		#endregion
	}
}
